package com.sufen.common.exception;


import com.sufen.common.reponse.CodeMsg;
import com.sufen.common.reponse.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import javax.servlet.http.HttpServletRequest;
import java.net.BindException;

/**
 * @description: 异常切面处理
 * @function:
 * @author: Liu Menglei
 * @Date: 2021/11/1 0001 21:39
 */
//@ControllerAdvice
//@ResponseBody
public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    public Result<String> exceptionHandler(HttpServletRequest request, Exception e){
        logger.error("ExceptionHandler Error: ",e);
        if (e instanceof GlobalException){
            System.out.println("~~~~~~~~~~~~~");
            return Result.error(((GlobalException) e).getCm());
        }
        if (e instanceof BindException){
            BindException exception = (BindException) e;
            String msg = exception.getMessage();
            return Result.error(CodeMsg.BIND_ERROR.fillArgs(msg));
        }
        return Result.error(CodeMsg.SERVER_ERROR);
    }

}